#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2

#构建一个HTTPHandler对象，处理http请求
http_handler = urllib2.HTTPHandler()
ua = {
    "Connection" : "keep-alive",
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
    "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "Accept-Language" : "zh-CN,zh;q=0.9",
}
#调用 urllib2的build_opener方法，用构建的HTTPHandler作为参数；
opener = urllib2.build_opener(http_handler)

request = urllib2.Request("http://www.xicidaili.com/", headers=ua)

#这个opener.open()方法其实就是替代了urllib2.urlopen()
response = opener.open(request)

print(response.read())